package com.zkingsoft.actions.admin.distribution;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.model.distribution.DisCouponRule;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DisCouponRuleService;
import com.zkingsoft.util.WebUtil;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-07-03 03:35
 */
@Controller
@RequestMapping(value = "admin/disCouponRule")
public class DisCouponRuleController extends BaseController {

	@Resource
	private DisCouponRuleService disCouponRuleService;

	@Resource
	private MatrixAdapter matrixAdapter;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "DisCouponRule_BEV";

	public static final String fnCode = "DisCouponRule";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(DisCouponRule disCouponRule, PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		return showList(disCouponRuleService, disCouponRule, pageVo);
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(DisCouponRule disCouponRule) {
		if (disCouponRule.getCouponRuleId() != null) {
			matrixAdapter.isBtnPermitted(edit);
			disCouponRule.setCouponRuleId(((DisCouponRule) WebUtil.getSessionAttribute(BEV)).getCouponRuleId());
			AjaxResult result = modify(disCouponRuleService, WebUtil.getSessionAttribute(BEV), disCouponRule,
					"优惠券规则");
			WebUtil.removeSessionAttribute(BEV);
			return result;
		} else {
			matrixAdapter.isBtnPermitted(add);
			return add(disCouponRuleService, disCouponRule, "优惠券规则");
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		matrixAdapter.isBtnPermitted(edit);
		DisCouponRule disCouponRule;
		if (id != null) {
			disCouponRule = disCouponRuleService.findById(id);
			WebUtil.getRequest().setAttribute("obj", disCouponRule);
			WebUtil.setSessionAttribute(BEV, disCouponRule);
		}
		return "admin/distribution/DisCouponRule-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		matrixAdapter.isBtnPermitted(del);
		return remove(disCouponRuleService, keys);
	}
	
	/**
	 * 获取所有的优惠券规则
	 */
	@RequestMapping(value = "/getAllRules")
	@ResponseBody
	public AjaxResult getAllRules() {
		List<DisCouponRule> dataList = disCouponRuleService.getAllRules();
		
		return new AjaxResult(AjaxResult.STATUS_OK, dataList);
	}
	

}